package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.XFDGoodsDeatailsActivity;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.ShopCarListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.ShopCarListPresenter;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：确认订单中嵌套的商品
 */
public class XFDConfirmOrderGoodsAdapter extends RecyclerView.Adapter<XFDConfirmOrderGoodsAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<ConfirmGoodBeen.DataBean.CartInfoBean.GoodsBean> mList;
    private Context mContext;
    private LayoutInflater mLayoutInflater;


    public XFDConfirmOrderGoodsAdapter(Context context, List<ConfirmGoodBeen.DataBean.CartInfoBean.GoodsBean> list) {
        this.mList = list;
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.item_confirm_order_good, null, false);

        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {

        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(mList.get(position).getProductInfo().getImage()).apply(options.transform(new CenterCrop(), roundedCorners)).into(holder.ivGoods);

        holder.tvGoodName.setText(mList.get(position).getProductInfo().getStore_name());
        if (mList.get(position).getStore().getIs_self_store().equals("1")) {//自营
            holder.tvSpecifications.setText("规格:"+mList.get(position).getProductInfo().getUnit_name());
            holder.tvNum.setText("数量:"+mList.get(position).getCart_num() + mList.get(position).getProductInfo().getUnit_name());
            holder.tvUnitPrice.setText("单价:¥ " + mList.get(position).getProductInfo().getPrice()+"/"+mList.get(position).getProductInfo().getUnit_name());
        }else {
            holder.tvSpecifications.setText("规格:" + mList.get(position).getProductInfo().getUnit_name());
            holder.tvNum.setText("数量:"+mList.get(position).getCart_num() + mList.get(position).getProductInfo().getUnit_name());
            holder.tvUnitPrice.setText("单价:¥ " + mList.get(position).getProductInfo().getPrice()+"/"+mList.get(position).getProductInfo().getUnit_name());
        }

        holder.tvPrice.setText("¥ " +mList.get(position).getGoodsTotalPrice());




    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_goods)
        ImageView ivGoods;
        @BindView(R.id.tv_specifications)
        TextView tvSpecifications;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_unit_price)
        TextView tvUnitPrice;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.tv_good_name)
        TextView tvGoodName;


        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
